// pages/feedback/index.js
import Ajax from '../../utils/request.js'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusBarHeight: app.globalData.statusBarHeight,
    questionList: [{
        name: '性能类型',
        value: '性能类型',
        id: 1,
      }, {
        name: '功能异常',
        value: '功能异常',
        id: 2,
      },
      {
        name: '产品建议',
        value: '产品建议',
        id: 3,
      },
      {
        name: '其它反馈',
        value: '其它反馈',
        id: 4,
      },
    ],
    activeItem: {},
    textNum: 0,
    textareaValue: '',
    uploadList: [],
    sourceType: ['album', 'camera'],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },
  // 提交按钮
  async submitEvent() {
    let {
      uploadList,
      activeItem,
      textareaValue
    } = this.data;
    let {
      phone
    } = app.globalData.userData
    let parmas = {
      suggest: textareaValue,
      contact: phone,
      images: uploadList,
      backType: activeItem.id
    }
    try {
      let res = await Ajax.post('/mini/feedback/addFeedback', parmas);
      if (res.code != 1) {
        wx.showToast({
          title: res.errorMsg,
          icon: 'none',
          duration: 1000
        })
        return false;
      }
      wx.showToast({
        title: '提交成功',
        duration: 1000
      });
      let timer = setTimeout(() => {
        wx.switchTab({
          url: '/pages/me/index',
        })
      }, 1000)
    } catch (error) {
      console.log(error)
    }
  },
  returnPageEvent() {
    wx.switchTab({
      url: '/pages/me/index',
    })
  },
  bindSelectEvent(event) {
    let item = event.currentTarget.dataset['item'];
    this.setData({
      activeItem: item,
    })
  },
  // 绑定输入框
  bindinputEvent(e) {
    let value = e.detail.value;
    this.setData({
      textareaValue: value,
      textNum: value.length
    })
  },
  frontimage: function() {
    var _this = this;
    var Type = _this.data.sourceType
    wx.chooseImage({
      count: 1, // 默认9 
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: Type, // 可以指定来源是相册还是相机，默认二者都有 
      success: function(res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片 
        _this.upLoadFileEvent(res.tempFilePaths)
      }
    })
  },
  // 图片移除
  removeImg(event) {
    let item = event.currentTarget.dataset['item'];
    let _list = this.data.uploadList || [];
    let _index = _list.findIndex(ele => ele == item);
    if (_index >= 0) {
      _list.splice(_index, 1)
      this.setData({
        uploadList: _list
      })
    }
  },
  // 图片上传
  async upLoadFileEvent(imgs, fileName) {
    try {
      let res = await Ajax.upload('/mini/upload/image', imgs[0]);
      let _result = res.data && res.data.imageUrls ? res.data.imageUrls[0] : '';
      let _list = this.data.uploadList || [];
      _list.push(_result);
      this.setData({
        uploadList: _list
      });
    } catch (error) {
      console.log(error)
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})